import java.util.*;

public class Stock {
	
	private Map<Part, Integer> parts;
	private Set<Observation> observer;
	
	public Stock(){
		parts = new HashMap<Part, Integer>();
		observer = new HashSet<Observation>();
	}
	
	public int get(Part t){
		
		if(parts.containsKey(t)==false){
			return -1;
		}
		
		return parts.get(t);	
	}

	
	public boolean set(Part t, int count){
		if(t==null || count<0){
			return false;
		}
		
		if(parts.containsKey(t)==false){
			return false;
		}
		parts.put(t, count);
		notify(t);
		return true;
	}
	
	public void insert(Part t, int count){
		if(parts.containsKey(t)==false){
			System.out.println(count);
			parts.put(t, count);
			System.out.println(parts.get(t));
			return;
		}
		int q = 0;
		q=count + parts.get(t);
		parts.put(t, q);
		notify(t);
	}
	
	public boolean delete(Part t, int count){
		if(parts.containsKey(t)==false){
			return false;
		}
		int q = parts.get(t) - count;
		if(q<0){
			return false;
		}
		parts.put(t, q);
		notify(t);
		return true;
	}
	
	public void register(Observation observer){
		if(!this.observer.contains(observer)){
			this.observer.add(observer);
		}
	}
	
	public void notify(Part t){
		for(Observation o : observer){
			o.alarm(t);
		}
	}

}
